package com.iwhalecloud.uncc.dao.impl;

import com.iwhalecloud.uncc.dao.ArchiverDao;
import com.iwhalecloud.uncc.db.DatasourceContextHolder;
import com.iwhalecloud.uncc.model.InsDistributeFileLog;
import com.iwhalecloud.uncc.utils.BatchUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author zq
 * @date 2021/10/24 1:33
 * @description
 */
@Repository("insDistributeFileLogArchiverDao")
public class insDistributeFileLogArchiverDaoImpl implements ArchiverDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Object query(Object o) {

        return null;
    }

    @Override
    public List queryList(Object o) {
        StringBuilder sql = new StringBuilder("SELECT * FROM INS_DISTRIBUTE_FILE_LOG WHERE TASK_INST_ID = ?");
        List<InsDistributeFileLog> list = jdbcTemplate.query(sql.toString(), new Object[]{o} , new BeanPropertyRowMapper<>(InsDistributeFileLog.class));
        if (!list.isEmpty()) {
            return list;
        }
        return null;
    }

    @Override
    public List batchQuery(List list) {
        StringBuilder sql = new StringBuilder("SELECT");
        sql.append(" `ID`, `TASK_INST_ID`, `FILE_FULL_PATH`, `SERVICE_INST_ID`, `ADAPT_ID`, `TEMPLATE_ID`, `SOURCE_ID`, `DISTRIBUTE_IP`, `DISTRIBUTE_PORT`, `COLLECT_TIME`, `CREATE_TIME`, `PARTITION_CODE`");
        sql.append(" FROM INS_DISTRIBUTE_FILE_LOG WHERE TASK_INST_ID in (:i)");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        List<InsDistributeFileLog> res = BatchUtil.batchQueryBeans(sql,list,new RowMapper() {
            @Override
            public Object mapRow(ResultSet resultSet, int i) throws SQLException {
                InsDistributeFileLog insDistributeFileLog = new InsDistributeFileLog();
                insDistributeFileLog.setId(resultSet.getLong("ID"));
                insDistributeFileLog.setTaskInstId(resultSet.getInt("TASK_INST_ID"));
                insDistributeFileLog.setFileFullPath(resultSet.getString("FILE_FULL_PATH"));
                insDistributeFileLog.setServiceInstId(resultSet.getInt("SERVICE_INST_ID"));
                insDistributeFileLog.setAdaptId(resultSet.getInt("ADAPT_ID"));
                insDistributeFileLog.setTemplateId(resultSet.getInt("TEMPLATE_ID"));
                insDistributeFileLog.setSourceId(resultSet.getInt("SOURCE_ID"));
                insDistributeFileLog.setDistributeIp(resultSet.getString("DISTRIBUTE_IP"));
                insDistributeFileLog.setDistributePort(resultSet.getInt("DISTRIBUTE_PORT"));
                insDistributeFileLog.setCreateTime(resultSet.getTimestamp("CREATE_TIME"));
                insDistributeFileLog.setCollectTime(resultSet.getString("COLLECT_TIME"));
                insDistributeFileLog.setPartitionCode(resultSet.getString("PARTITION_CODE"));
                return insDistributeFileLog;
            }
        });
        return res;
    }

    @Override
    public void insert(Object o) {

    }

    @Override
    public void batchInsert(List list) {
        StringBuilder sql = new StringBuilder("INSERT INTO `ins_distribute_file_log`");
        sql.append(" (`ID`, `TASK_INST_ID`, `FILE_FULL_PATH`, `SERVICE_INST_ID`, `ADAPT_ID`, `TEMPLATE_ID`, `SOURCE_ID`, `DISTRIBUTE_IP`, `DISTRIBUTE_PORT`, `COLLECT_TIME`, `CREATE_TIME`, `PARTITION_CODE`, `ARCHIVER_TIME`)");
        sql.append(" VALUES (:id,:taskInstId,:fileFullPath,:serviceInstId,:adaptId,:templateId,:sourceId,:distributeIp,:distributePort,:collectTime,:createTime,:partitionCode,:archiverTime)");
        BatchUtil.batchUpdateBeans(sql,list);
    }

    @Override
    public void update(int flag, Object o) {

    }

    @Override
    public void batchUpdate(int flag, List list) {
        StringBuilder sql = new StringBuilder("UPDATE INS_DISTRIBUTE_FILE_LOG SET ARCHIVER_FLAG = ");
        sql.append(flag);
        sql.append(" WHERE ID = :id");
        BatchUtil.batchUpdateBeans(sql,list);
    }

    @Override
    public void delete(Object o) {

    }

    @Override
    public void batchDelete(List list) {
        StringBuilder sql = new StringBuilder("DELETE FROM INS_DISTRIBUTE_FILE_LOG WHERE ID = :id");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        BatchUtil.batchUpdateBeans(sql,list);
    }
}
